"use strict";
// pages/room/edit.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    name: "",
    barcode: "",
    default_roomid:"",
    agefrom: "",
    ageto:"",
    remark:"",
    accountIndex:0,
    accounts:['请选择','声光','桌游','操作','其他']
  },
  refreshInfo: function() {

    if (this.data.id > 0){
      app.api.api_toy_edit_info({ id: this.data.id}).then(res => {
        this.setData({
          name: res.name,
          barcode: res.barcode,
          accountIndex: res.type,
          agefrom: res.agefrom,
          ageto: res.ageto,
          remark:res.remark
        })
      })
    }
  },
  bindAccountChange: function(e){
    this.setData({ accountIndex: e.detail.value })
  },
  bindKeyInput: function(e) {
    this.data[e.currentTarget.dataset.obj] = e.detail.value
  },
  save: function() {
    app.api.api_toy_edit_save({
        id: this.data.id,
        name: this.data.name,
        barcode: this.data.barcode,
        type: this.data.accountIndex,
        agefrom: this.data.agefrom,
        ageto: this.data.ageto,
        remark: this.data.remark
      }).then(res => {
        wx.navigateBack()
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    if(id){
      this.setData({ id: parseInt(id)})
      app.api.api_enum_listarr("toycolumns").then(res=>{
        this.setData({accounts:res})
        this.refreshInfo()
      })      
    }
  },

  scan_code:function(){
    var that = this;
    app.api.api_scan(function(res){
      if(res.errMsg == 'scanCode:ok'){
        that.setData({barcode:res.result})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.refreshInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})